import { defineStore } from 'pinia';

import { IfileListData } from '@/views/documentCenter/config/fileListData'
import { getDocumentList } from '@/apis/system/document'
import { ElMessage } from 'element-plus'

interface Isearch {
    code: string
    ruleType: string
    value: string | number
}

interface Iparams {
    search: Isearch[]
}

const allDocListData: IfileListData[] = []
const docListData: IfileListData[] = []
const docRichEditorFile = {} as IfileListData
// 定义容器
export const store = defineStore('docData', {
    state: () => {
        return {
            docListData, // 文档数据
            allDocListData, //所有文件
            loading: false,
            docRichEditorFile // 编辑文件
        }
    },
    getters: {},
    actions: {
        /**
         * @desc: 获取搜索的数据
         * @param {Iparams} params
         * @return {*}
         */
        async getData(data?: object) {
            this.$state.loading = true
            await getDocumentList(data)
                .then((res) => {
                    if (!res) return
                    let arr: any = []
                    arr = res
                    arr.forEach((element: IfileListData) => {
                        element.created_at = new Date(
                            +new Date(new Date(element.created_at).toJSON()) + 8 * 3600 * 1000
                        )
                            .toISOString()
                            .replace(/T/g, ' ')
                            .replace(/\.[\d]{3}Z/, '')
                    })
                    this.$state.docListData = arr
                })

                .catch(() => {
                    ElMessage('服务器内部错误')
                })
                .finally(() => {
                    this.$state.loading = false
                })
        }
    },
});